package com.lc.afbx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.PostApiInsuranceInfo;
import com.lc.afbx.util.DateTimePickDialogBefore;
import com.lc.afbx.util.MyToast;
import com.lc.afbx.util.PictureUtil;
import com.lc.afbx.util.PrefrenceConfig;
import com.lc.afbx.util.Validator;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilBase64;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/5/23.
 */
public class CompulsoryInsuranceInsureActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.goods_address_qiangxian_rl) private RelativeLayout goods_address_qiangxian_rl;

    private Button btnQueRenTouBao;
    private CheckBox dianZiQuan_img;
    private CheckBox zhiQuan_img;

    private EditText managerNum;
    private Bundle bundle;
    private TextView total;
    private TextView endTime;
    private TextView startTime;
    private TextView insurance_company;
    private TextView insurance_stores;
    private TextView insurance_type;
    private TextView insured;
    private TextView factory_plate_model;
    private EditText address;
    private EditText email;
    private ImageView imgYinLian;

    private int i;//这个是判断用的

    private PostApiInsuranceInfo postApiInsuranceInfo = new PostApiInsuranceInfo(new AsyCallBack<PostApiInsuranceInfo.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostApiInsuranceInfo.Info info) throws Exception {

            UtilToast.show(context, "投保成功");

            startActivity(new Intent(CompulsoryInsuranceInsureActivity.this, PayWebActivity.class).putExtra("type", "2").putExtra("ordersn", info.ordersn));

            BaseApplication.INSTANCE.finishActivity(CompulsoryInsuranceActivity.class, CompulsoryInsuranceChooseCompanyActivity.class, CompulsoryInsuranceFillInfoActivity.class, CompulsoryInsurancePriceActivity.class, CompulsoryInsuranceInsureActivity.class);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouye_jiaoqiangxian_querentoubao);

        bundle = getIntent().getExtras();
        initView();

        setListener();
    }

    private void setListener() {

        btnQueRenTouBao.setOnClickListener(this);
        startTime.setOnClickListener(this);
        imgYinLian.setOnClickListener(this);


    }

    private void initView() {

        findViewById(R.id.back).setOnClickListener(this);
        btnQueRenTouBao = ((Button) findViewById(R.id.shouye_btn_jiaoqiangxian_querentoubao_toubao));

//        findViewById(R.id.rLayout_dianzi).setOnClickListener(this);
        findViewById(R.id.rLayout_zhi).setOnClickListener(this);

        address = ((EditText) findViewById(R.id.goods_address_qiangxian));
        email = ((EditText) findViewById(R.id.email_qiangxian));
        managerNum = ((EditText) findViewById(R.id.manager_number));//TODO 这个应该是还得改
        //
        total = ((TextView) findViewById(R.id.total_querenbaodan));

        endTime = ((TextView) findViewById(R.id.endTime));
        startTime = ((TextView) findViewById(R.id.startTime));
        insurance_company = ((TextView) findViewById(R.id.insurance_company));
        insurance_stores = ((TextView) findViewById(R.id.insurance_stores));
        insured = ((TextView) findViewById(R.id.insured));
        factory_plate_model = ((TextView) findViewById(R.id.factory_plate_model));

        total.setText(bundle.getString("heji") + "元");
        insurance_company.setText(bundle.getString("companyName"));
        insurance_stores.setText(bundle.getString("shopName"));
        insured.setText(bundle.getString("insured"));
        factory_plate_model.setText(bundle.getString("model_number"));


        insurance_type = ((TextView) findViewById(R.id.insurance_type));
        if (bundle.getString("insurance_type").equals("1")) {
            insurance_type.setText("新保");
            insurance_type.setTextColor(getResources().getColor(R.color.xinbao));
        } else {
            insurance_type.setText("续保");
            insurance_type.setTextColor(getResources().getColor(R.color.xubao));
        }

        imgYinLian = ((ImageView) findViewById(R.id.img_yinLian_jiaoQiangXian));
        imgYinLian.setTag(true);

        //这部分是电子保单  纸质保单
        dianZiQuan_img = ((CheckBox) findViewById(R.id.quanquan1));
        zhiQuan_img = ((CheckBox) findViewById(R.id.quanquan2));

        if (!TextUtils.isEmpty(BaseApplication.BasePreferences.readCode())) {

            managerNum.setEnabled(false);
            managerNum.setText(BaseApplication.BasePreferences.readCode());

        }

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:
                finish();
                return;

            case R.id.startTime:

                // 选择起保时间
                DateTimePickDialogBefore dialogBefore = new DateTimePickDialogBefore(context, endTime);
                dialogBefore.dateTimePicKDialog(startTime);

                return;

            case R.id.endTime:
                //不知道写不写
//                dialogBefore = new DateTimePickDialogBefore(context);
//                dialogBefore.dateTimePicKDialog(endTime);
                return;

            case R.id.img_yinLian_jiaoQiangXian:

//                if (i++ % 2 == 0) {
//                    imgYinLian.setImageResource(R.mipmap.xubaovb);
//                    imgYinLian.setTag(true);
//                } else {
//                    imgYinLian.setImageResource(R.mipmap.xb);
//                    imgYinLian.setTag(false);
//                }

                return;

            case R.id.rLayout_dianzi:

//                if (!dianZiQuan_img.isChecked()) {
//
//                    dianZiQuan_img.setChecked(true);
//
//                } else {
//
//                    dianZiQuan_img.setChecked(false);
//
//                }

                return;

            case R.id.rLayout_zhi:

                if (!zhiQuan_img.isChecked()) {

                    zhiQuan_img.setChecked(true);
                    goods_address_qiangxian_rl.setVisibility(View.VISIBLE);

                } else {

                    zhiQuan_img.setChecked(false);
                    goods_address_qiangxian_rl.setVisibility(View.GONE);
                    address.setText("");

                }

                return;

            case R.id.shouye_btn_jiaoqiangxian_querentoubao_toubao:


                if (TextUtils.isEmpty(startTime.getText().toString())) {
                    MyToast.showToast("起保时间 不能为空", context);
                    return;
                }

                if (dianZiQuan_img.isChecked() && zhiQuan_img.isChecked()) {

                    postApiInsuranceInfo.warranty_type = "1,2";

                } else if (dianZiQuan_img.isChecked()) {
                    postApiInsuranceInfo.warranty_type = "1";
                } else if (zhiQuan_img.isChecked()) {
                    postApiInsuranceInfo.warranty_type = "2";
                } else {

                    MyToast.showToast("请选择保单形式", context);
                    return;
                }

                if (TextUtils.isEmpty(email.getText().toString())) {
                    MyToast.showToast("邮箱 不能为空", context);
                    return;
                }
                if (zhiQuan_img.isChecked() && TextUtils.isEmpty(address.getText().toString().replaceAll(" ", ""))) {
                    MyToast.showToast("配送地址 不能为空", context);
                    return;
                }
                if (((boolean) imgYinLian.getTag()) == false) {
                    MyToast.showToast("请选择 支付方式", context);
                    return;
                }

                if (!Validator.isEmail(email.getText().toString())) {
                    MyToast.showToast("邮箱 格式不正确", context);
                    return;
                }


                UtilLog.e("===========================3", bundle.getString("insurance_type") + "sdfasdfsaasdfas");

                postApiInsuranceInfo.userid = BaseApplication.BasePreferences.readUserId();
                postApiInsuranceInfo.cid = bundle.getString("cid");
                postApiInsuranceInfo.id_number = bundle.getString("id_number");
                postApiInsuranceInfo.address = bundle.getString("address");
                postApiInsuranceInfo.tel = bundle.getString("tel");
                postApiInsuranceInfo.plate_number = bundle.getString("plate_number");
                postApiInsuranceInfo.cartype = bundle.getString("cartype");
                postApiInsuranceInfo.usenature = bundle.getString("usenature");
                postApiInsuranceInfo.engine_number = bundle.getString("engine_number");
                postApiInsuranceInfo.vin = bundle.getString("vin");
                postApiInsuranceInfo.model_number = bundle.getString("model_number");
                postApiInsuranceInfo.passengers = bundle.getString("passengers");
                postApiInsuranceInfo.weight = bundle.getString("weight");
                postApiInsuranceInfo.displacement = bundle.getString("displacement");
                postApiInsuranceInfo.power = bundle.getString("power");
                postApiInsuranceInfo.vehicle_weight = bundle.getString("vehicle_weight");
                postApiInsuranceInfo.taxpayer_number = bundle.getString("taxpayer_number");

                postApiInsuranceInfo.insurance_type = bundle.getString("insurance_type");

                //保单形式的参数  在验证那里
//                if ((boolean) dianZiQuan_img.getTag()) {
//                    postApiInsuranceInfo.warranty_type = PrefrenceConfig.ONE;
//                } else {
//                    postApiInsuranceInfo.warranty_type = PrefrenceConfig.TWO;
//                }

                postApiInsuranceInfo.regtime = bundle.getString("regtime");


                postApiInsuranceInfo.drive_url = PictureUtil.bitmapToString(bundle.getString("drive_url"));
                postApiInsuranceInfo.id_url = PictureUtil.bitmapToString(bundle.getString("id_url"));

                postApiInsuranceInfo.Customer_Manager_number = managerNum.getText().toString();
                postApiInsuranceInfo.dispatching = address.getText().toString();
                postApiInsuranceInfo.email = email.getText().toString();
                postApiInsuranceInfo.business_id = bundle.getString("shopId");
                postApiInsuranceInfo.name = bundle.getString("insured");
                postApiInsuranceInfo.nowfee = "0";
                postApiInsuranceInfo.backfee = "0";
                postApiInsuranceInfo.fine = "0";
                postApiInsuranceInfo.premium = bundle.getString("heji");
                postApiInsuranceInfo.starttime = startTime.getText().toString();
                postApiInsuranceInfo.endtime = endTime.getText().toString();


                postApiInsuranceInfo.execute(this);

                break;

        }

    }


}
